/**
 * Created by wangp on 2016/8/22.
 */
$(function(){
    //注册验证
    function validate(){
        var validate = $('#sign-form').validate();
        var ck = validate.checkForm();
        if( validate.check($('input[name="mobile"]')) && !$('.getcode').hasClass('disabled') ){
            $('.getcode').removeAttr('disabled');
        }else{
            $('.getcode').attr('disabled', true);
        }
        if( ck ){
            $('.sign-btn').removeAttr('disabled');
        }else {
            $('.sign-btn').attr('disabled', true);
        }
    }
    //删除按钮tap事件，验证form表单
    $('#sign-form').on('tap','.mui-icon-clear',function(){
        validate();
    });
    //输入框keyup事件，验证form表单
    $('#sign-form .mui-input-row .mui-input').on('keyup', function(){
        validate();
    });

    //发送短信
    $('.getcode').click(function(){
        if( $(this).hasClass('fpwd') )  return false;
        send_code($(this),'loginout');
    });
    //注册点击事件
    $('#btn-signup').click(function(){
        var mobile = $('#sign-form input[name="mobile"]').val(),
            code = $('#sign-form input[name="code"]').val(),
            password = $('#sign-form input[name="password"]').val();
        var data = {
            apiCode: '_register_001',
            mobile: mobile,
            code: code,
            loginPass: password
        };
        http(data, function(responed){
            if( responed.error == 0 ){
                mui.toast('恭喜您，注册成功', 1000);
                setTimeout(function(){
                    location.href = 'user.html';
                }, 1000);
            }else{
                mui.alert(responed.info);
            }
        });
    });

    //登录点击事件
    $('#btn-signin').on('tap',function(){
        var mobile = $('#sign-form input[name="mobile"]').val(),
            password = $('#sign-form input[name="password"]').val();
        var data = {
            apiCode: '_login_001',
            username: mobile,
            loginPass: password
        };
        http(data, function(responed){
            if( responed.error == 0 ){
                mui.toast('登陆成功', 1000);
                $.cookie('userInfo', JSON.stringify(responed.info));
                setTimeout(function(){
                    location.href = 'user.html';
                }, 1000);
            }else{
                mui.alert(responed.info);
            }
        });
    });
    //忘记密码点击事件
    $('#forgetPwd').click(function(){
        var mobile = $('#sign-form input[name="mobile"]').val(),
            code = $('#sign-form input[name="code"]').val(),
            newPwd = $('#sign-form input[name="newPwd"]').val(),
            sureNewPwd = $('#sign-form input[name="sureNewPwd"]').val();
        var data = {
            apiCode: '_set_pwd_003',
            mobile: mobile,
            code: code,
            newPwd: newPwd,
            sureNewPwd: sureNewPwd
        };
        http(data, function(responed){
            if( responed.error == 0 ){
                mui.toast('密码设置成功', 1000);
                setTimeout(function(){
                    location.href = 'signin.html';
                }, 1000);
            }else{
                mui.alert(responed.info);
            }
        });
    });
});
function send_code(self, tp, opt){
    var mobile = $('#sign-form input[name="mobile"]');
    if( !mobile.val() ){
        mui.alert('请输入手机号码！');
        mobile.focus();
        return false;
    }
    var data = {
        apiCode: '_sms_002',
        mobile: mobile.val()
    };
    if( tp == 'loginin' ){
        data.apiCode = '_sms_001';
        data.userToken = opt.userToken;
    }
    http(data, function(responed){
        if( responed.error == 0 ){
            mui.toast('发送成功');
            var ms = 59;
            self.attr('disabled', true).addClass('disabled').text('还剩60秒');
            //创建倒计时
            var interval = setInterval(function(){
                self.text('还剩'+(ms--)+'秒');
            }, 1000);
            //60秒后清楚计时器
            setTimeout(function(){
                clearInterval(interval);
                self.removeAttr('disabled').removeClass('disabled').text('获取验证码');
                return true;
            }, 60000)
        }else{
            mui.alert(responed.info);
        }
    });
}

